cmake_minimum_required(VERSION 3.20)
project(GateServer)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)

# 查找依赖包
find_package(Protobuf REQUIRED)
find_package(PkgConfig REQUIRED)
find_package(spdlog REQUIRED)
find_package(yaml-cpp REQUIRED)

# 使用 pkg-config 查找 gRPC（推荐）
pkg_check_modules(gRPC REQUIRED grpc++ grpc)
pkg_check_modules(HIREDIS REQUIRED hiredis)

# 查找 MySQL 库
find_library(MYSQLCPPCONN_LIBRARY
    NAMES mysqlcppconn
    PATHS /usr/lib/x86_64-linux-gnu /usr/lib
)

find_library(MYSQLCLIENT_LIBRARY
    NAMES mysqlclient
    PATHS /usr/lib/x86_64-linux-gnu /usr/lib
)

find_library(JSONCPP_LIBRARY
    NAMES jsoncpp
    PATHS /usr/lib/x86_64-linux-gnu /usr/lib
)

# 设置 proto 文件
set(PROTO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/proto/message.proto)

# 生成 gRPC 代码
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})

# 查找 gRPC C++ 插件
find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin)

if(GRPC_CPP_PLUGIN)
    # 生成 gRPC 代码
    set(GRPC_SRCS "${CMAKE_CURRENT_BINARY_DIR}/message.grpc.pb.cc")
    set(GRPC_HDRS "${CMAKE_CURRENT_BINARY_DIR}/message.grpc.pb.h")
    
    add_custom_command(
        OUTPUT "${GRPC_SRCS}" "${GRPC_HDRS}"
        COMMAND ${Protobuf_PROTOC_EXECUTABLE}
        ARGS --grpc_out=${CMAKE_CURRENT_BINARY_DIR}
             --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN}
             -I ${CMAKE_CURRENT_SOURCE_DIR}/proto
             ${PROTO_FILES}
        DEPENDS ${PROTO_FILES}
    )
else()
    message(FATAL_ERROR "grpc_cpp_plugin not found")
endif()

# 设置源文件
set(SRC_LIST
    src/net/Cserver.cc
    src/net/httpconnection.cc
    src/net/logicsystem.cc
    src/rpc/varifygrpcclient.cc
    src/util/config.cc
    src/net/asioservicepool.cc
    src/redis/redismgr.cc
    src/redis/redisconnectionpool.cc
    src/mysql/mysqlconnectionpool.cc
    src/mysql/mysqldao.cc
    src/mysql/mysqlmgr.cc
    src/service/Userservice.cc
    src/util/httpresponsebuilder.cc
    src/validator/registervalidator.cc
    src/service/Userservice.cc
    src/entity/user.cc
    src/validator/resetpswvalidator.cc
    ${PROTO_SRCS}
    ${GRPC_SRCS}
    src/statusclient/statusclient.cc
    src/statusclient/statusconpool.cc
    src/validator/loginvalidtor.cc
)

# 创建静态库
add_library(serverlib STATIC ${SRC_LIST})

# 设置包含目录
target_include_directories(serverlib PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_BINARY_DIR}
    ${Protobuf_INCLUDE_DIRS}
    ${gRPC_INCLUDE_DIRS}
    ${YAML_CPP_INCLUDE_DIR}
    ${HIREDIS_INCLUDE_DIRS}
    # MySQL 头文件路径需要明确指定
    /usr/include/mysql-cppconn-8
    /usr/include/mysql
)

# 链接库
target_link_libraries(serverlib PUBLIC
    ${JSONCPP_LIBRARY}
    ${Protobuf_LIBRARIES}
    ${gRPC_LIBRARIES}
    pthread
    dl
    spdlog::spdlog
    yaml-cpp::yaml-cpp
    ${HIREDIS_LIBRARIES}
    ${MYSQLCLIENT_LIBRARY}
    ${MYSQLCPPCONN_LIBRARY}
)

# 设置输出目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

# 创建测试可执行文件
add_executable(test_server tests/test_server.cc)
target_link_libraries(test_server PRIVATE serverlib)

add_executable(test_log tests/test_log.cc)
target_link_libraries(test_log PRIVATE serverlib)

add_executable(test_config tests/test_config.cc)
target_link_libraries(test_config PRIVATE serverlib)

add_executable(test_redis tests/test_redis.cc)
target_link_libraries(test_redis PRIVATE serverlib)

add_executable(test_mysql tests/test_mysql.cc)
target_link_libraries(test_mysql PRIVATE serverlib)